package com.jieshuibao.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jieshuibao.common.config.Const;
import com.jieshuibao.model.db.ArticleUserKey;
import com.jieshuibao.model.db.AttentionExample;
import com.jieshuibao.model.db.AttentionExample.Criteria;
import com.jieshuibao.model.db.CmsArticle;
import com.jieshuibao.model.db.CmsCategory;
import com.jieshuibao.model.db.CountAndSumMoney;
import com.jieshuibao.model.db.Duty;
import com.jieshuibao.model.db.RedEnvelopesArticleUser;
import com.jieshuibao.model.domain.AddAdmireResponse;
import com.jieshuibao.model.domain.AddArticleRequest;
import com.jieshuibao.model.domain.AllArticleResponse;
import com.jieshuibao.model.domain.ArticelOppAndAppRequest;
import com.jieshuibao.model.domain.Article;
import com.jieshuibao.model.domain.ArticleCategory;
import com.jieshuibao.model.domain.ArticleCategoryResponse;
import com.jieshuibao.model.domain.ArticleDetailResponse;
import com.jieshuibao.model.domain.ArticleDiscussRequest;
import com.jieshuibao.model.domain.CodeDesResponse;
import com.jieshuibao.model.domain.ConllectionArticleRequest;
import com.jieshuibao.model.domain.CookieUser;
import com.jieshuibao.model.domain.PagePhone;
import com.jieshuibao.model.domain.PageRequest;
import com.jieshuibao.model.domain.RedEnvelopesUserArticleResponse;
import com.jieshuibao.model.domain.UserLevel;
import com.jieshuibao.service.ArticelService;
import com.jieshuibao.service.AttentionService;
import com.jieshuibao.service.CmsArticleService;
import com.jieshuibao.service.CounselorDutyService;
import com.jieshuibao.service.PushService;
import com.jieshuibao.service.RedEnvelopesService;
import com.jieshuibao.service.RedisService;
import com.jieshuibao.service.SmsSendService;
import com.jieshuibao.util.Base64ForImg;
import com.jieshuibao.util.DateUtils;
import com.jieshuibao.util.DeleteHtmlUtil;
import com.jieshuibao.util.StringUtils;
import com.jieshuibao.util.UUIDGenerator;
import com.jieshuibao.util.push.PushType;

/**
 * 文章接口控制器
 * @author LISS
 *
 */
@Controller
@RequestMapping(value = "/article")
public class ArticleController {
	private static final Logger logger = LoggerFactory.getLogger("ArticleController.class");
	private static final ObjectMapper mapper = new ObjectMapper();

	@Resource
	private ArticelService articelService;
	
	@Resource
	private RedisService redisService;
	
	@Resource
	private CmsArticleService cmsArticleService;

	@Resource
	private CounselorDutyService counselorDutyService;
	
	@Resource
	private RedEnvelopesService redEnvelopesService;
	
	@Resource
	private AttentionService attentionService;
	@Resource
	PushService pushService;
	@Resource
	private SmsSendService smsSendService;
	
	@Value("${images.basePath}")
	private String imagesBasePath;
	
	@Value("${article.images.path}")
	private String articleImagesPath;
	
	private static final String appId = "8aaf07086619653001661b1865b301fa";

	/**
	 * 文章列表接口/最新文章
	 * @param request
	 * @param pageRequest {sinceId:数据起始索引,maxId:数据结束索引,uuid:标签的uuid（如果是点击标签查询必传)}
	 * @return
	 */
	@RequestMapping(value = "/all", method = RequestMethod.POST)
	@ResponseBody
	public Object queryAllArticle(HttpServletRequest request,
			@RequestBody PageRequest pageRequest) {

		AllArticleResponse allArticleResponse = new AllArticleResponse();
		try {
			logger.info("接收：" + mapper.writeValueAsString(pageRequest));
				//获取用户信息
				CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
				
				if (cookieUser != null) {
					//获取手机号码
					String phoneNumber = cookieUser.getPhoneNumber();
					if (phoneNumber != null) {
						//数据起始索引
						int sinceId = pageRequest.getSinceId();
						//数据结束索引
						int maxId = pageRequest.getMaxId();
						//数据索引-逻辑处理
						if (sinceId <= 0) {
							sinceId = 1;
						}
						if (maxId <= 0 || maxId < sinceId) {
							maxId = sinceId;
						}
						//标签的uuid（如果是点击标签查询必传)
						String uuid = pageRequest.getUuid();
						List<Article> articles = null;
						if (uuid != null && !"".equals(uuid)) {
							//根据文章标签查询文章
							PagePhone pagePhone = new PagePhone();
							pagePhone.setPageIndex(sinceId - 1);
							pagePhone.setPageSize(maxId - sinceId + 1);
							pagePhone.setId(uuid);
							articles = articelService.getArticlesByCategoryId(pagePhone);
						} else {
							//分页查询文章
							articles = articelService.getArticle(sinceId - 1,maxId - sinceId + 1);
						}
						//存在文章结果集
						if (articles != null) {
							for (Article article : articles) {
								//获取描述、摘要
								String descriptionHtml = article.getDescription();
								if(descriptionHtml != null && !"".equals(descriptionHtml)){
									//处理描述、摘要（提取纯文字）
									String description = DeleteHtmlUtil.getTextFromHtml(descriptionHtml);
									article.setDescription(description);
								}
								//获取用户级别信息
								UserLevel userLevel = article.getUserLevel();
								if (userLevel != null) {
									//获取-咨询师职责
									String counselorDuty = userLevel.getCounselorDuty();
									if (counselorDuty != null && !"".equals(counselorDuty)) {
										//判断咨询师职责-是否为空字符串（是否存在咨询师职责）
										if (!counselorDuty.contains(" ")) {
											StringBuilder stringBuilder = new StringBuilder();
											String[] split = counselorDuty.split(",");
											if(split!=null){
												for (String id : split) {
													if (id != null && !"".equals(id)) {
														//获取职责名称
														Duty duty = counselorDutyService.getDutyById(id);
														if (duty != null) {
															stringBuilder.append(duty.getDutyName() + " ");
														}
													}
												}
												//更新-咨询师职责
												userLevel.setCounselorDuty(stringBuilder.toString());
											}
										}
									}
									//更新-文章中用户级别信息
									article.setUserLevel(userLevel);
								}
							}
						}
						allArticleResponse.setArticles(articles);
						allArticleResponse.setSinceId(sinceId);
						allArticleResponse.setMaxId(sinceId + articles.size() - 1);
						allArticleResponse.setRows(articles.size());
						allArticleResponse.setCode("1");
					} else {
						// 重新登录
						allArticleResponse.setCode("2");
						allArticleResponse.setDes("重新登录");
					}
				} else {
					// 重新登录
					allArticleResponse.setCode("2");
					allArticleResponse.setDes("重新登录");
				}
			
			logger.info("返回：" + mapper.writeValueAsString(allArticleResponse));
		} catch (Exception e) {
			e.printStackTrace();
			allArticleResponse.setCode("0");
			allArticleResponse.setDes("系统繁忙");
		}
		return allArticleResponse;
	}
	/**
	 * 最热文章/个人下的所有文章
	 * @param request
	 * @param pageRequest {articleType:"hot"-最热文章/"personal"-个人文章,
	 * 		  uuid:文章作者的手机号（点击个人文章是必填）,sinceId:起始索引,maxId:结束索引}
	 * @return
	 */
	@RequestMapping(value = "/condition", method = RequestMethod.POST)
	@ResponseBody
	public Object conditionAllArticle(HttpServletRequest request,
			@RequestBody PageRequest pageRequest) {

		AllArticleResponse allArticleResponse = new AllArticleResponse();
		try {
			logger.info("接收：" + mapper.writeValueAsString(pageRequest));
				//获取用户信息
				CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
				if (cookieUser != null) {
					//获取手机号码
					String phoneNumber = cookieUser.getPhoneNumber();
					if (phoneNumber != null) {
						//数据起始索引
						int sinceId = pageRequest.getSinceId();
						//数据结束索引
						int maxId = pageRequest.getMaxId();
						//获取文章作者的手机号（点击个人文章是必填）
						String uuid = pageRequest.getUuid();
						//数据索引-逻辑处理
						if (sinceId <= 0) {
							sinceId = 1;
						}
						if (maxId <= 0 || maxId < sinceId) {
							maxId = sinceId;
						}
						//设置分页信息
						PagePhone pagePhone = new PagePhone();
						pagePhone.setPageIndex(sinceId - 1);
						pagePhone.setPageSize(maxId - sinceId + 1);
						//获取操作类型-"hot"-最热文章/"personal"-个人文章
						String articleType = pageRequest.getArticleType();
						
						List<Article> articles = null;
						if(articleType == null || "".equals(articleType)){
							allArticleResponse.setCode("3");
							allArticleResponse.setDes("数据格式错误");
							return allArticleResponse;
						}else if("hot".equals(articleType)){
							//查询最热文章，并设置返回代码
							articles = articelService.getArticlesByHot(pagePhone);
							allArticleResponse.setCode("1");
						}else if("personal".equals(articleType)){
							//根据手机号查询个人文章
							pagePhone.setId(uuid);
							articles = articelService.getArticleByPhone(pagePhone);
							//设置关注量查询条件
							AttentionExample attentionExample = new AttentionExample();
							Criteria createCriteria = attentionExample.createCriteria();
							//被关注者
							createCriteria.andBeAttentionNumEqualTo(uuid);//被点击个人文章的用户手机号（主键）
							//关注者
							createCriteria.andAttentionNumEqualTo(phoneNumber);//当前登录人的手机号
							//被点击个人文章的用户是否当前用户关注
							int countAttention = attentionService.countAttention(attentionExample);
							if(countAttention>0){
								//设置关注标识
								allArticleResponse.setIsAttention(1);
							}
							//设置返回状态代码
							allArticleResponse.setCode("1");
						}else {
							allArticleResponse.setCode("3");
							allArticleResponse.setDes("数据格式错误");
							return allArticleResponse;
						}
						//存在文章
						if (articles != null) {
							for (Article article : articles) {
								//获取文章描述、摘要
								String descriptionHtml = article.getDescription();
								if(descriptionHtml!=null&&!descriptionHtml.equals("")){
									//处理文章描述、摘要
									String description = DeleteHtmlUtil.getTextFromHtml(descriptionHtml);
									//更新文章描述、摘要
									article.setDescription(description);
								}
								//获取用户级别信息
								UserLevel userLevel = article.getUserLevel();
								if (userLevel != null) {
									//获取-咨询师职责
									String counselorDuty = userLevel.getCounselorDuty();
									if (counselorDuty != null && !"".equals(counselorDuty)) {
										//判断咨询师职责-是否为空字符串（是否存在咨询师职责）
										if (!counselorDuty.contains(" ")) {
											StringBuilder stringBuilder = new StringBuilder();
											String[] split = counselorDuty.split(",");
											if(split!=null){
												for (String id : split) {
													if (id != null && !"".equals(id)) {
														//获取职责名称
														Duty duty = counselorDutyService.getDutyById(id);
														if (duty != null) {
															stringBuilder.append(duty.getDutyName() + " ");
														}
													}
												}
												//更新-咨询师职责
												userLevel.setCounselorDuty(stringBuilder.toString());
											}
										}
									}
									//更新-文章中用户级别信息
									article.setUserLevel(userLevel);
								}
							}
						}
						allArticleResponse.setArticles(articles);
						allArticleResponse.setSinceId(sinceId);
						allArticleResponse.setMaxId(sinceId + articles.size() - 1);
						allArticleResponse.setRows(articles.size());
						allArticleResponse.setCode("1");
					} else {
						// 重新登录
						allArticleResponse.setCode("2");
						allArticleResponse.setDes("重新登录");
					}
				} else {
					// 重新登录
					allArticleResponse.setCode("2");
					allArticleResponse.setDes("重新登录");
				}
			
			logger.info("返回：" + mapper.writeValueAsString(allArticleResponse));
		} catch (Exception e) {
			e.printStackTrace();
			allArticleResponse.setCode("0");
			allArticleResponse.setDes("系统繁忙");
		}
		return allArticleResponse;
	}
	// 文章列表接口-分享
	@RequestMapping(value = "/allShare", method = RequestMethod.POST)
	@ResponseBody
	public Object queryAllArticleShare(HttpServletRequest request,
			@RequestBody PageRequest pageRequest) {
		AllArticleResponse allArticleResponse = new AllArticleResponse();
		try {
			logger.info("接收：" + mapper.writeValueAsString(pageRequest));

			int sinceId = pageRequest.getSinceId();
			int maxId = pageRequest.getMaxId();
			if (sinceId <= 0) {
				sinceId = 1;
			}
			if (maxId <= 0 || maxId < sinceId) {
				maxId = sinceId;
			}
			String uuid = pageRequest.getUuid();
			List<Article> articles = new ArrayList<Article>();
			if (uuid != null && !uuid.equals("")) {
				PagePhone pagePhone = new PagePhone();

				pagePhone.setPageIndex(sinceId - 1);
				pagePhone.setPageSize(maxId - sinceId + 1);
				pagePhone.setId(uuid);
				articles = articelService.getArticlesByCategoryId(pagePhone);
			} else {

				articles = articelService.getArticle(sinceId - 1, maxId
						- sinceId + 1);
			}
			if (articles != null) {
				for (Article article : articles) {
					String descriptionHtml = article.getDescription();
					if(descriptionHtml!=null&&!descriptionHtml.equals("")){
						String description = DeleteHtmlUtil.getTextFromHtml(descriptionHtml);
						article.setDescription(description);
					}
					UserLevel userLevel = article.getUserLevel();
					if (userLevel != null) {
						String counselorDuty = userLevel.getCounselorDuty();
						if (counselorDuty != null && !counselorDuty.equals("")) {
							if (!counselorDuty.contains(" ")) {
								StringBuilder stringBuilder = new StringBuilder();
								String[] split = counselorDuty.split(",");
								if(split!=null){
									
									for (String id : split) {
										if (id != null && !id.equals("")) {
											Duty duty = counselorDutyService
													.getDutyById(id);
											if (duty != null) {
												
												stringBuilder.append(duty
														.getDutyName() + " ");
											}
										}
										
									}
									userLevel.setCounselorDuty(stringBuilder
											.toString());
								}
							}
						}

						article.setUserLevel(userLevel);
					}
				}
			}
			allArticleResponse.setArticles(articles);
			allArticleResponse.setSinceId(sinceId);
			allArticleResponse.setMaxId(sinceId + articles.size() - 1);
			allArticleResponse.setRows(articles.size());

			allArticleResponse.setCode("1");
			logger.info("返回：" + mapper.writeValueAsString(allArticleResponse));
		} catch (Exception e) {
			e.printStackTrace();
			allArticleResponse.setCode("0");
			allArticleResponse.setDes("系统繁忙");
		}
		return allArticleResponse;
	}
	/**
	 * 文章标签
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/category", method = RequestMethod.POST)
	@ResponseBody
	public Object queryArticleCategory(HttpServletRequest request) {
		ArticleCategoryResponse articleCategoryResponse = new ArticleCategoryResponse();
		try {
				//获取当前用户
				CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
				if (cookieUser != null) {
					//查询栏目表
					List<ArticleCategory> articleCategory = articelService.getArticleCategory("1");
					articleCategoryResponse.setCategories(articleCategory);
					articleCategoryResponse.setCode("1");
				} else {
					// 重新登录
					articleCategoryResponse.setCode("2");
					articleCategoryResponse.setDes("重新登录");
				}
			
		} catch (Exception e) {
			e.printStackTrace();
			articleCategoryResponse.setCode("0");
			articleCategoryResponse.setDes("系统繁忙");
		}
		return articleCategoryResponse;
	}
	/**
	 * 发布文章
	 * @param request
	 * @param addArticleRequest {title:文章标题,content:文章内容,coverImage:图片的bass64字符串,categoryId:标签的id,description:摘要}
	 * @return
	 */
	@RequestMapping(value = "/addArticle", method = RequestMethod.POST)
	@ResponseBody
	public Object addArticle(HttpServletRequest request,
			@RequestBody AddArticleRequest addArticleRequest) {
		AddAdmireResponse addArticleResponse = new AddAdmireResponse();
		try {
			logger.debug("接收：" + mapper.writeValueAsString(addArticleRequest));
			
				CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
				if (cookieUser != null) {
					//获取用户手机号码
					String phoneNumber = cookieUser.getPhoneNumber();
					//获取文章内容
					String content = addArticleRequest.getContent();
					//获取标签的id
					String categoryId = addArticleRequest.getCategoryId();
					//获取文章标题
					String title = addArticleRequest.getTitle();
					//获取图片的bass64字符串
					String coverImage = addArticleRequest.getCoverImage();
					//获取摘要
					String description = addArticleRequest.getDescription();
					//参数校验
					if (phoneNumber != null && title != null && !"".equals(title) && (!StringUtils.isBlank(description)|| !StringUtils.isBlank(content))) {
						String uuid = UUIDGenerator.getUUID();
						logger.info("接收文章uuid：" + uuid);
						System.out.println("接收文章uuid：" + uuid);
						//组装文章数据
						CmsArticle cmsArticle = new CmsArticle();
						cmsArticle.setId(uuid);//主键
						cmsArticle.setTitle(title);//标题
						
						Date date = new Date();
						//设置删除标记
						boolean k=date.before(DateUtils.getStartTime()) || date.after(DateUtils.getEndTime());
						if(date.before(DateUtils.getStartTime()) || date.after(DateUtils.getEndTime())){
							cmsArticle.setDelFlag("1");
						}else {
							cmsArticle.setDelFlag("0");
						}
						//设置时间
						cmsArticle.setCreateDate(date);
						//创建人
						cmsArticle.setCreatBy(phoneNumber);
						if (categoryId != null) {
							//设置标签ID
							cmsArticle.setCategoryId(categoryId);
						}
						//处理图片
						if (coverImage != null && !"".equals(coverImage)) {
							Base64ForImg.GenerateImage(coverImage,imagesBasePath + articleImagesPath + uuid + "_cover");
							cmsArticle.setCoverImage(uuid + "_cover");
						}
						if(StringUtils.isBlank(description)){
							//描述、摘要
							cmsArticle.setDescription("<font style=\"line-height:1.5;\">"+content+"</font>");
						}else{
							//描述、摘要
							cmsArticle.setDescription("<font style=\"line-height:1.5;\">"+description+"</font>");
						}
						//增加文章
						cmsArticleService.addCmsArticle(cmsArticle);
						//设置返回数据
						addArticleResponse.setCode("1");
						addArticleResponse.setCredateDate(date);
						addArticleResponse.setTime(date.getTime());
						addArticleResponse.setDes("发布成功");
						
						//查询粉丝，进行推送
						AttentionExample attentionExample = new AttentionExample();
						Criteria createCriteria = attentionExample.createCriteria();
						//被关注者
						createCriteria.andBeAttentionNumEqualTo(phoneNumber);//被点击个人文章的用户手机号（主键）
						//当前用户的所有粉丝
						if (!k) {
							List<RedEnvelopesArticleUser> redEnvelopesArticleUsers = attentionService.getUserFans(attentionExample);
							if(redEnvelopesArticleUsers != null && redEnvelopesArticleUsers.size() > 0){
								String[] str1 = {uuid,cookieUser.getUserName(),title};
								StringBuffer phones = new StringBuffer();
								for(int i=0;i<redEnvelopesArticleUsers.size();i++){
									RedEnvelopesArticleUser redEnvelopesArticleUser = redEnvelopesArticleUsers.get(i);
									//发布推送
									pushService.pushAlias(PushType.MODEL_6, str1, redEnvelopesArticleUser.getUuid(), "1");
									if(i == (redEnvelopesArticleUsers.size()-1)){
										phones.append(redEnvelopesArticleUser.getPhone());
									}else{
										phones.append(redEnvelopesArticleUser.getPhone()).append(",");
									}
								}
								smsSendService.sendNotify(appId,phones.toString(),Const.SCENE_NEW_ARTICLE,
										new String[]{cookieUser.getUserName(),cmsArticle.getTitle()});
							}
						}
						
					} else {
						// 数据格式错误
						addArticleResponse.setCode("3");
						addArticleResponse.setDes("数据格式错误");
					}
				} else {
					// 重新登录
					addArticleResponse.setCode("2");
					addArticleResponse.setDes("重新登录");
				}
			
			logger.info("返回：" + mapper.writeValueAsString(addArticleResponse));
		} catch (Exception e) {
			e.printStackTrace();
			addArticleResponse.setCode("0");
			addArticleResponse.setDes("系统繁忙");
		}
		return addArticleResponse;
	}
	/**
	 * 收藏文章接口
	 * @param request
	 * @param conllectionArticleRequest {"articleUuid":文章的ID}
	 * @return
	 */
	@RequestMapping(value = "/collection", method = RequestMethod.POST)
	@ResponseBody
	public Object collectionArticle(HttpServletRequest request,
			@RequestBody ConllectionArticleRequest conllectionArticleRequest) {
		CodeDesResponse codeDesResponse = new CodeDesResponse();
		try {
			logger.info("接收：" + mapper.writeValueAsString(conllectionArticleRequest));
				//获取当前用户
				CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
				if (cookieUser != null) {
					//获取用户电话号码
					String phoneNumber = cookieUser.getPhoneNumber();
					if (phoneNumber != null) {
						//获取文章ID
						String articleUuid = conllectionArticleRequest.getArticleUuid();
						if (articleUuid != null && !articleUuid.equals("")) {
							//获取文章数据
							Article article = articelService.getArticleById(articleUuid);
							if (article != null) {
								ArticleUserKey articleUserKey = new ArticleUserKey();
								articleUserKey.setArticleUuid(articleUuid);
								articleUserKey.setUserPhoneNum(phoneNumber);
								//查询是否已存在收藏数据
								ArticleUserKey articleUserKey2 = articelService.getArticleUserKey(articleUserKey);
								//判断并返回相应信息
								if (articleUserKey2 == null) {
									//添加收藏
									articelService.addArticleUser(articleUserKey);
									codeDesResponse.setCode("1");
									codeDesResponse.setDes("收藏成功");
								} else {
									codeDesResponse.setCode("1");
									codeDesResponse.setDes("已经收藏过");
								}
							} else {
								// 数据格式错误
								codeDesResponse.setCode("3");
								codeDesResponse.setDes("数据格式错误！");
							}
						} else {
							// 数据格式错误
							codeDesResponse.setCode("3");
							codeDesResponse.setDes("数据格式错误！！");
						}
					} else {
						// 重新登录
						codeDesResponse.setCode("2");
						codeDesResponse.setDes("重新登录");
					}
				} else {
					// 重新登录
					codeDesResponse.setCode("2");
					codeDesResponse.setDes("重新登录");
				}
			
			logger.info("返回：" + mapper.writeValueAsString(codeDesResponse));
		} catch (Exception e) {
			e.printStackTrace();
			codeDesResponse.setCode("0");
			codeDesResponse.setDes("系统繁忙");
		}
		return codeDesResponse;
	}
	/**
	 * 取消收藏文章接口
	 * @param request
	 * @param conllectionArticleRequest {"articleUuid":文章uuid}
	 * @return
	 */
	@RequestMapping(value = "/cancel", method = RequestMethod.POST)
	@ResponseBody
	public Object cancelCollectionArticle(HttpServletRequest request,
			@RequestBody ConllectionArticleRequest conllectionArticleRequest) {
		CodeDesResponse codeDesResponse = new CodeDesResponse();
		try {
			logger.info("接收：" + mapper.writeValueAsString(conllectionArticleRequest));
				//获取当前登录用户
				CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
				if (cookieUser != null) {
					//获取用户手机号码
					String phoneNumber = cookieUser.getPhoneNumber();
					if (phoneNumber != null) {
						//获取文章ID
						String articleUuid = conllectionArticleRequest.getArticleUuid();
						if (articleUuid != null && !articleUuid.equals("")) {
							ArticleUserKey articleUserKey = new ArticleUserKey();
							articleUserKey.setArticleUuid(articleUuid);
							articleUserKey.setUserPhoneNum(phoneNumber);
							//查询是否已存在收藏数据
							ArticleUserKey articleUserKey2 = articelService.getArticleUserKey(articleUserKey);
							if(articleUserKey2!=null){
								//删除收藏数据
								articelService.delArticleUserKey(articleUserKey);
								// 数据格式错误
								codeDesResponse.setCode("1");
								codeDesResponse.setDes("取消成功");
							}else {
								// 数据格式错误
								codeDesResponse.setCode("3");
								codeDesResponse.setDes("数据格式错误！");
							}
						} else {
							// 数据格式错误
							codeDesResponse.setCode("3");
							codeDesResponse.setDes("数据格式错误！！");
						}
					} else {
						// 重新登录
						codeDesResponse.setCode("2");
						codeDesResponse.setDes("重新登录");
					}
				} else {
					// 重新登录
					codeDesResponse.setCode("2");
					codeDesResponse.setDes("重新登录");
				}
			
			logger.info("返回：" + mapper.writeValueAsString(codeDesResponse));
		} catch (Exception e) {
			e.printStackTrace();
			codeDesResponse.setCode("0");
			codeDesResponse.setDes("系统繁忙");
		}
		return codeDesResponse;
	}
	/**
	 * 删除文章
	 * @param request
	 * @param conllectionArticleRequest {"articleUuid":文章ID}
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public Object deleteArticle(HttpServletRequest request,
			@RequestBody ConllectionArticleRequest conllectionArticleRequest) {
		CodeDesResponse codeDesResponse = new CodeDesResponse();
		try {
			logger.info("接收：" + mapper.writeValueAsString(conllectionArticleRequest));
				//获取当前用户
				CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
				if (cookieUser != null) {
					//获取用户手机号码
					String phoneNumber = cookieUser.getPhoneNumber();
					if (phoneNumber != null) {
						//获取文章ID
						String articleUuid = conllectionArticleRequest.getArticleUuid();
						if (articleUuid != null && !"".equals(articleUuid)) {
							//获取目标文章
							CmsArticle cmsArticle = cmsArticleService.getCmsArticleById(articleUuid);
							if(cmsArticle!=null){
								//获取创建人（创建人手机号码）
								String creatBy = cmsArticle.getCreatBy();
								//判断是否为本人文章
								if(phoneNumber.equals(creatBy)){
									//删除文章
									articelService.delAricle(articleUuid);
									codeDesResponse.setCode("1");
									codeDesResponse.setDes("删除成功");
								}else {
									codeDesResponse.setCode("3");
									codeDesResponse.setDes("你不能删除此文章");
								}
							}else {
								codeDesResponse.setCode("3");
								codeDesResponse.setDes("数据格式错误！！！");
							}
						} else {
							// 数据格式错误
							codeDesResponse.setCode("3");
							codeDesResponse.setDes("数据格式错误！！");
						}
					} else {
						// 重新登录
						codeDesResponse.setCode("2");
						codeDesResponse.setDes("重新登录");
					}
				} else {
					// 重新登录
					codeDesResponse.setCode("2");
					codeDesResponse.setDes("重新登录");
				}
			//} else {
				// 重新登录
				//codeDesResponse.setCode("2");
				//codeDesResponse.setDes("重新登录");
			//}
			logger.info("返回：" + mapper.writeValueAsString(codeDesResponse));
		} catch (Exception e) {
			e.printStackTrace();
			codeDesResponse.setCode("0");
			codeDesResponse.setDes("系统繁忙");
		}
		return codeDesResponse;
	}
	/**
	 * 赞与不攒文章
	 * @param request
	 * @param articelOppAndAppRequest {"articleUuid":文章的ID,"status":1-赞/2-不赞}
	 * @return
	 */
	@RequestMapping(value="/proveAndpose", method=RequestMethod.POST)
	@ResponseBody
	public Object proveAndPose(HttpServletRequest request,
			@RequestBody ArticelOppAndAppRequest articelOppAndAppRequest) {
		CodeDesResponse codeDesResponse = new CodeDesResponse();
		try {
			logger.info("接收：" + mapper.writeValueAsString(articelOppAndAppRequest));
			//获取当前用户
			CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
				if (cookieUser != null) {
					//获取用户手机号码
					String phoneNum = cookieUser.getPhoneNumber();
					if (phoneNum != null) {
						//获取文章ID
						String aritcleUuid = articelOppAndAppRequest.getArticleUuid();
						//获取操作类型
						int status = articelOppAndAppRequest.getStatus();
						if (aritcleUuid != null && !"".equals(aritcleUuid)) {
							//根据文章ID查询文章
							CmsArticle cmsArticleById = cmsArticleService.getCmsArticleById(aritcleUuid);
							if (cmsArticleById != null) {
								CmsArticle cmsArticle = new CmsArticle();
								cmsArticle.setId(aritcleUuid);
								if (status == 1) {
									Integer approveNum = cmsArticleById.getApproveNum();
									cmsArticle.setApproveNum(approveNum + 1);
								} else if (status == 2) {
									Integer opposeNum = cmsArticleById.getOpposeNum();
									cmsArticle.setOpposeNum(opposeNum + 1);
								} else {
									codeDesResponse.setCode("3");
									codeDesResponse.setDes("数据格式错误");
									return codeDesResponse;
								}
								//更新文章
								cmsArticleService.updateCmsArticle(cmsArticle);
								codeDesResponse.setCode("1");
								codeDesResponse.setDes(status == 1 ? "已赞" : "已踩");
							} else {
								// 数据格式错误
								codeDesResponse.setCode("3");
								codeDesResponse.setDes("数据格式错误");
							}
						} else {
							// 数据格式错误
							codeDesResponse.setCode("3");
							codeDesResponse.setDes("数据格式错误");
						}
					} else {
						// 重新登录
						codeDesResponse.setCode("2");
						codeDesResponse.setDes("重新登录");
					}
				} else {
					codeDesResponse.setCode("2");
					codeDesResponse.setDes("重新登录");
				}
			
			logger.info("返回：" + mapper.writeValueAsString(codeDesResponse));
		} catch (Exception e) {
			e.printStackTrace();
			codeDesResponse.setCode("0");
			codeDesResponse.setDes("系统繁忙");
		}
		return codeDesResponse;
	}
	/**
	 * 查询文章详情
	 * @param request
	 * @param articleDetailsRequest {articleUuid:文章的id}
	 * @return
	 */
	@RequestMapping(value = "/articleDetails", method = RequestMethod.POST)
	@ResponseBody
	public Object articleDetails(HttpServletRequest request,
			@RequestBody ArticleDiscussRequest articleDetailsRequest) {
		ArticleDetailResponse articleDetailResponse = new ArticleDetailResponse();
		try {
			logger.info("接收：" + mapper.writeValueAsString(articleDetailsRequest));
				//获取当前用户
				CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
				if (cookieUser != null) {
					//获取手机号码
					String phoneNumber = cookieUser.getPhoneNumber();
					if (phoneNumber != null) {
						//获取文章主键
						String articleUuid = articleDetailsRequest.getArticleUuid();
						if (articleUuid != null && !"".equals(articleUuid)) {
							//根据主键获取文章
							Article article = articelService.getArticleAndIfCollectionById(articleUuid,phoneNumber);
							if (article != null) {
								//String content = article.getContent();
								//获取描述、摘要
								String description = article.getDescription();
								//获取标题
								String title = article.getTitle();
								if(description != null && !"".equals(description)){
									//更新内容
									articleDetailResponse.setContent(description);
									//将描述的HTML数据中， 标签全部去掉存入descriptionText中
									String descriptionText = DeleteHtmlUtil.getTextFromHtml(article.getDescription());
									String descriptionTextTwo  = descriptionText.length() > 200 ? descriptionText.substring(0, 200) : descriptionText;
									articleDetailResponse.setDescriptionText(descriptionTextTwo);
								}
								if (title != null && !"".equals(title)) {
									//更新标题
									articleDetailResponse.setTitle(title);
								}
								//判断文章是否是本人所写
								String creatBy = article.getCreatBy();
								//相等 是本人所写
								if(phoneNumber.equals(creatBy)){  
									articleDetailResponse.setIsCreateByOwn(1);
								}else{
									articleDetailResponse.setIsCreateByOwn(0);
								}
								
								Article articleById = articelService.getArticleById(articleUuid);
								//用户信息
								UserLevel userLevel = articleById.getUserLevel();
								
								StringBuilder stringBuilder = new StringBuilder();
								String counselorDuty = userLevel.getCounselorDuty();
								if (counselorDuty  != null
										&& !counselorDuty.equals("")) {
									String[] split = counselorDuty.split(",");

									for (String id : split) {
										if (id != null && !id.equals("")) {
											Duty duty = counselorDutyService
													.getDutyById(id);
											if (duty != null) {

												stringBuilder.append(duty
														.getDutyName() + " ");
											}
										}

									}
									userLevel.setCounselorDuty(stringBuilder
											.toString());
								}
								articleDetailResponse.setUserLevel(userLevel);
								
								String articleCategoryId = article.getCategory().getUuid();
								CmsCategory cmsCategorys = cmsArticleService.getCmsCategoryById(articleCategoryId);
								//文章变签名和ID
								ArticleCategory articleCategory = new ArticleCategory();
								articleCategory.setUuid(articleCategoryId);
								articleCategory.setName(cmsCategorys.getName());
								articleDetailResponse.setCategorys(articleCategory);
								//时间
								articleDetailResponse.setCretateDate(article.getCreateDate().getTime());
								articleDetailResponse.setTime(article.getCreateDate().getTime());

								articleDetailResponse.setIfCollection(article.getIfCollection());
								//查询文章红包总个数以及红包总额
								CountAndSumMoney countAndSumMoney = redEnvelopesService.getCountAndSumMoney(articleUuid);
								//发红包人的头像
								List<String> userHeadImages = redEnvelopesService.getUserHeadImages(articleUuid);
								if(countAndSumMoney != null){
									//更新红包总个数
									articleDetailResponse.setCount(countAndSumMoney.getCount());
									//更新红包总金额
									articleDetailResponse.setSum(countAndSumMoney.getSum());
								}
								if(userHeadImages != null){
									//更新发红包人头像
									articleDetailResponse.setHeadImages(userHeadImages);
								}
								
								//设置关注量查询条件
								AttentionExample attentionExample = new AttentionExample();
								Criteria createCriteria = attentionExample.createCriteria();
								//被关注者
								createCriteria.andBeAttentionNumEqualTo(article.getCreatBy());//被点击个人文章的用户手机号（主键）
								//关注者
								createCriteria.andAttentionNumEqualTo(phoneNumber);//当前登录人的手机号
								//被点击个人文章的用户是否当前用户关注
								int countAttention = attentionService.countAttention(attentionExample);
								if(countAttention>0){
									//设置关注标识
									articleDetailResponse.setIsAttention(1);
								}
								//是否已收藏
								articleDetailResponse.setCode("1");
							} else {
								// 数据格式错误
								articleDetailResponse.setCode("3");
								articleDetailResponse.setDes("数据格式错误");
							}
						} else {
							// 数据格式错误
							articleDetailResponse.setCode("3");
							articleDetailResponse.setDes("数据格式错误");
						}
					} else {
						// 重新登录
						articleDetailResponse.setCode("2");
						articleDetailResponse.setDes("重新登录");
					}
				} else {
					// 重新登录
					articleDetailResponse.setCode("2");
					articleDetailResponse.setDes("重新登录");
				}
			
			logger.debug("返回："
					+ mapper.writeValueAsString(articleDetailResponse));
		} catch (Exception e) {
			e.printStackTrace();
			articleDetailResponse.setCode("0");
			articleDetailResponse.setDes("系统繁忙");
		}
		return articleDetailResponse;
	}
	// 文章详情-分享
	@RequestMapping(value = "/articleDetailsShare", method = RequestMethod.POST)
	@ResponseBody
	public Object articleDetailsShare(HttpServletRequest request,
			@RequestBody ArticleDiscussRequest articleDetailsRequest) {
		ArticleDetailResponse articleDetailResponse = new ArticleDetailResponse();
		try {
			logger.info("接收：" + mapper.writeValueAsString(articleDetailsRequest));
			//获取文章ID
			String articleUuid = articleDetailsRequest.getArticleUuid();
			if (articleUuid != null && !articleUuid.equals("")) {
				//获取文章数据
				Article article = articelService.getArticleById(articleUuid);
				if (article != null) {
					//获取描述、摘要
					String description = article.getDescription();
					//获取名称
					String title = article.getTitle();
					//获取栏目数据
					ArticleCategory category = article.getCategory();
					//获取用户级别
					UserLevel userLevel = article.getUserLevel();
					//返回对象设置用户名称和头像
					articleDetailResponse.setName(userLevel.getRealName());
					articleDetailResponse.setHeadImage(userLevel.getHeadImage());
					if(category!=null){
						String name = category.getName();
						//返回对象设置用户名称和头像
						articleDetailResponse.setCategory(name);
					}
					Date createDate = article.getCreateDate();
					//返回对象设置创建时间
					articleDetailResponse.setCretateDate(createDate.getTime());
					//判断描述、摘要
					if(description != null && !"".equals(description)){
						//返回对象设置描述、摘要
						articleDetailResponse.setContent(description);
					}
					//判断标题
					if (title != null && !"".equals(title)) {
						//返回对象设置标题
						articleDetailResponse.setTitle(title);
					}
					articleDetailResponse.setCode("1");
				} else {
					//数据格式错误
					articleDetailResponse.setCode("3");
					articleDetailResponse.setDes("数据格式错误");
				}
			} else {
				// 数据格式错误
				articleDetailResponse.setCode("3");
				articleDetailResponse.setDes("数据格式错误");
			}
			logger.debug("返回：" + mapper.writeValueAsString(articleDetailResponse));
		} catch (Exception e) {
			e.printStackTrace();
			articleDetailResponse.setCode("0");
			articleDetailResponse.setDes("系统繁忙");
		}
		return articleDetailResponse;
	}
	/**
	 * 收藏文章列表
	 * @param request
	 * @param pageRequest {"sinceId":起始索引,"maxId":结束索引}
	 * @return
	 */
	@RequestMapping(value = "/articleCollection", method = RequestMethod.POST)
	@ResponseBody
	public Object articleCollection(HttpServletRequest request,
			@RequestBody PageRequest pageRequest) {
		AllArticleResponse articleCollectionResponse = new AllArticleResponse();
		try {
			//获取当前用户
			CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
				if (cookieUser != null) {
					//获取用户手机号码
					String phoneNum = cookieUser.getPhoneNumber();
					if (phoneNum != null) {
						//获取起始索引和结束索引
						int maxId = pageRequest.getMaxId();
						int sinceId = pageRequest.getSinceId();
						//处理起始索引和结束索引
						if (sinceId <= 0) {
							sinceId = 1;
						}
						if (maxId <= 0 || maxId < sinceId) {
							maxId = sinceId;
						}
						//设置分页信息
						PagePhone pagePhone = new PagePhone();
						pagePhone.setPageIndex(sinceId - 1);
						pagePhone.setPageSize(maxId - sinceId + 1);
						pagePhone.setPhoneNum(phoneNum);
						//查询收藏文章
						List<Article> articles = articelService.getCollectionArticle(pagePhone);
						if(articles!=null){
							for (Article article : articles) {
								//获取用户级别
								UserLevel userLevel = article.getUserLevel();
								if (userLevel != null) {
									//获取咨询师职责
									String counselorDuty = userLevel.getCounselorDuty();
									if (counselorDuty != null && !"".equals(counselorDuty)) {
										//判断咨询师职责-是否为空字符串（是否存在咨询师职责）
										if (!counselorDuty.contains(" ")) {
											StringBuilder stringBuilder = new StringBuilder();
											String[] split = counselorDuty.split(",");
											for (String id : split) {
												if (id != null && !"".equals(id)) {
													//获取咨询师职责名称
													Duty duty = counselorDutyService.getDutyById(id);
													if (duty != null) {
														stringBuilder.append(duty.getDutyName() + " ");
													}
												}
											}
											//更新咨询师职责
											userLevel.setCounselorDuty(stringBuilder.toString());
										}
									}
									//更新用户级别
									article.setUserLevel(userLevel);
								}
							}
						}
						//设置返回数据
						articleCollectionResponse.setSinceId(sinceId);
						articleCollectionResponse.setMaxId(sinceId + articles.size() - 1);
						articleCollectionResponse.setRows(articles.size());
						articleCollectionResponse.setArticles(articles);
						articleCollectionResponse.setCode("1");
					} else {
						// 重新登录
						articleCollectionResponse.setCode("2");
						articleCollectionResponse.setDes("重新登录");
					}
				} else {
					articleCollectionResponse.setCode("2");
					articleCollectionResponse.setDes("重新登录");
				}
			
			logger.info("返回：" + mapper.writeValueAsString(articleCollectionResponse));
		} catch (Exception e) {
			e.printStackTrace();
			articleCollectionResponse.setCode("0");
			articleCollectionResponse.setDes("系统繁忙");
		}
		return articleCollectionResponse;
	}
	/**
	 * 根据标签查询文章queryArticleByCategoryId
	 * @param request
	 * @param pageRequest {uuid:标签id,sinceid:起始索引,maxid:结束索引}
	 * @return
	 */
	@RequestMapping(value = "/articleCategoryId", method = RequestMethod.POST)
	@ResponseBody
	public Object queryArticleByCategoryId(HttpServletRequest request,
			@RequestBody PageRequest pageRequest) {
		AllArticleResponse articleResponse = new AllArticleResponse();
		try {
			//获取当前用户
			CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
				if (cookieUser != null) {
					//获取用户手机号码
					String phoneNum = cookieUser.getPhoneNumber();
					if (phoneNum != null) {
						//获取、处理并设置分页信息
						int maxId = pageRequest.getMaxId();
						int sinceId = pageRequest.getSinceId();
						String id = pageRequest.getUuid();
						if (sinceId <= 0) {
							sinceId = 1;
						}
						if (maxId <= 0 || maxId < sinceId) {
							maxId = sinceId;
						}
						PagePhone pagePhone = new PagePhone();
						pagePhone.setPageIndex(sinceId - 1);
						pagePhone.setPageSize(maxId - sinceId + 1);
						
						if (id != null && !"".equals(id)) {
							pagePhone.setId(id);//标签ID
							//根据标签ID查询文章
							List<Article> articles = articelService.getArticlesByCategoryId(pagePhone);
							if (articles != null) {
								for (Article article : articles) {
									//获取用户级别
									UserLevel userLevel = article.getUserLevel();
									if (userLevel != null) {
										//获取咨询师职责
										String counselorDuty = userLevel.getCounselorDuty();
										if (counselorDuty != null && !"".equals(counselorDuty)) {
											if (!counselorDuty.contains(" ")) {
												StringBuilder stringBuilder = new StringBuilder();
												String[] split = counselorDuty.split(",");
												for (String ids : split) {
													if (ids != null && !"".equals(ids)) {
														//获取咨询师职责名称
														Duty duty = counselorDutyService.getDutyById(ids);
														if (duty != null) {
															stringBuilder.append(duty.getDutyName() + " ");
														}
													}
												}
												//更新咨询师职责名称
												userLevel.setCounselorDuty(stringBuilder.toString());
											}
										}
										//更新用户级别
										article.setUserLevel(userLevel);
									}
									//是否已关注作者
									//设置关注量查询条件
									AttentionExample attentionExample = new AttentionExample();
									Criteria createCriteria = attentionExample.createCriteria();
									//被关注者
									createCriteria.andBeAttentionNumEqualTo(article.getCreatBy());//被点击个人文章的用户手机号（主键）
									//关注者
									createCriteria.andAttentionNumEqualTo(phoneNum);//当前登录人的手机号
									//被点击个人文章的用户是否当前用户关注
									int countAttention = attentionService.countAttention(attentionExample);
									if(countAttention>0){
										//设置关注标识
										article.setIsAttention(1);
									}
								}
							}
							//设置返回数据
							articleResponse.setArticles(articles);
							articleResponse.setCode("1");
							articleResponse.setSinceId(sinceId);
							articleResponse.setMaxId(sinceId + articles.size() - 1);
							articleResponse.setRows(articles.size());
						} else {
							// 数据格式错误
							articleResponse.setCode("3");
							articleResponse.setDes("数据格式错误");
						}
					} else {
						// 重新登录
						articleResponse.setCode("2");
						articleResponse.setDes("重新登录");
					}
				} else {
					articleResponse.setCode("2");
					articleResponse.setDes("重新登录");
				}
			
			logger.info("返回：" + mapper.writeValueAsString(articleResponse));
		} catch (Exception e) {
			e.printStackTrace();
			articleResponse.setCode("0");
			articleResponse.setDes("系统繁忙");
		}
		return articleResponse;
	}
	/**
	 * 点击查看打赏人员接口
	 * @param request
	 * @param pageRequest {"uuid":文章的uuid,"sinceId":起始索引,"maxId":结束索引}
	 * @return
	 */
	@RequestMapping(value = "/redEnvelopesUser", method = RequestMethod.POST)
	@ResponseBody
	public Object queryUserRedEnvelopesByArticleId(HttpServletRequest request,
			@RequestBody PageRequest pageRequest) {
		RedEnvelopesUserArticleResponse redEnvelopesUserArticleResponse = new RedEnvelopesUserArticleResponse();
		try {
			//获取当前用户
			CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
				if (cookieUser != null) {
					//获取用户手机号码
					String phoneNum = cookieUser.getPhoneNumber();
					if (phoneNum != null) {
						//获取起始索引和结束索引
						int maxId = pageRequest.getMaxId();
						int sinceId = pageRequest.getSinceId();
						String id = pageRequest.getUuid();
						//处理起始索引和结束索引
						if (sinceId <= 0) {
							sinceId = 1;
						}
						if (maxId <= 0 || maxId < sinceId) {
							maxId = sinceId;
						}
						//设置分页信息
						PagePhone pagePhone = new PagePhone();
						pagePhone.setPageIndex(sinceId - 1);
						pagePhone.setPageSize(maxId - sinceId + 1);
						if (id != null && !"".equals(id)) {
							pagePhone.setId(id);
							//查询根据文章ID查询对应红包
							List<RedEnvelopesArticleUser> redEnvelopesArticleUsers = redEnvelopesService.getRedEnvelopesArticleUsers(pagePhone);
							if(redEnvelopesArticleUsers != null){
								for (RedEnvelopesArticleUser redEnvelopesArticleUser : redEnvelopesArticleUsers) {
									//咨询师职责
									String counselorDuty = redEnvelopesArticleUser.getCounselorDuty();
									if (counselorDuty != null && !"".equals(counselorDuty)) {
										StringBuilder stringBuilder = new StringBuilder();
										String[] split = counselorDuty.split(",");
										if(split != null){
											for (String ids : split) {
												if (ids != null && !"".equals(ids)) {
													//获取咨询师职责名称
													Duty duty = counselorDutyService.getDutyById(ids);
													if (duty != null) {
														stringBuilder.append(duty.getDutyName() + " ");
													}
												}
											}
											int i = stringBuilder.lastIndexOf(" ");
											stringBuilder.delete(i, i+1);
											//更新咨询师职责
											redEnvelopesArticleUser.setCounselorDuty(stringBuilder.toString());
										}
									}
								}
							}
							//设置返回数据
							redEnvelopesUserArticleResponse.setCode("1");
							redEnvelopesUserArticleResponse.setSinceId(sinceId);
							redEnvelopesUserArticleResponse.setMaxId(sinceId + redEnvelopesArticleUsers.size() - 1);
							redEnvelopesUserArticleResponse.setRows(redEnvelopesArticleUsers.size());
							redEnvelopesUserArticleResponse.setRedEnvelopesArticleUser(redEnvelopesArticleUsers);
						} else {
							// 数据格式错误
							redEnvelopesUserArticleResponse.setCode("3");
							redEnvelopesUserArticleResponse.setDes(" 数据格式错误");
						}
					} else {
						// 重新登录
						redEnvelopesUserArticleResponse.setCode("2");
						redEnvelopesUserArticleResponse.setDes("重新登录");
					}
				} else {
					redEnvelopesUserArticleResponse.setCode("2");
					redEnvelopesUserArticleResponse.setDes("重新登录");
				}
			
			logger.info("返回：" + mapper.writeValueAsString(redEnvelopesUserArticleResponse));
		} catch (Exception e) {
			e.printStackTrace();
			redEnvelopesUserArticleResponse.setCode("0");
			redEnvelopesUserArticleResponse.setDes("系统繁忙");
		}
		return redEnvelopesUserArticleResponse;
	}
	
	/**
	 * 模糊搜索文章接口
	 * @param request
	 * @param pageRequest {sinceId:数据起始索引,maxId:数据结束索引,search:文章标题}
	 * @return
	 */
	@RequestMapping(value = "/search", method = RequestMethod.POST)
	@ResponseBody
	public Object searchArticle(HttpServletRequest request,
			@RequestBody PageRequest pageRequest) {

		AllArticleResponse allArticleResponse = new AllArticleResponse();
		try {
			logger.info("接收：" + mapper.writeValueAsString(pageRequest));
				//获取用户信息
				CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
				if (cookieUser != null) {
					//获取手机号码
					String phoneNumber = cookieUser.getPhoneNumber();
					if (phoneNumber != null) {
						//数据起始索引
						int sinceId = pageRequest.getSinceId();
						//数据结束索引
						int maxId = pageRequest.getMaxId();
						//数据索引-逻辑处理
						if (sinceId <= 0) {
							sinceId = 1;
						}
						if (maxId <= 0 || maxId < sinceId) {
							maxId = sinceId;
						}
						//获取要模糊查询的文章标题
						String searchTitle = pageRequest.getSearch();
						List<Article> articles = new ArrayList<Article>();
						if (searchTitle != null && !"".equals(searchTitle)) {
							//根据文章标签查询文章
							PagePhone pagePhone = new PagePhone();
							pagePhone.setPageIndex(sinceId - 1);
							pagePhone.setPageSize(maxId - sinceId + 1);
							pagePhone.setSearch(searchTitle);
							//根据title模糊查询文章
							articles = articelService.getArticleByTitle(pagePhone);
						}
						//存在文章结果集
						if (articles != null && articles.size() > 0) {
							for (Article article : articles) {
								//获取描述、摘要
								String descriptionHtml = article.getDescription();
								if(descriptionHtml != null && !"".equals(descriptionHtml)){
									//处理描述、摘要（提取纯文字）
									String description = DeleteHtmlUtil.getTextFromHtml(descriptionHtml);
									article.setDescription(description);
								}
								//获取用户级别信息
								UserLevel userLevel = article.getUserLevel();
								if (userLevel != null) {
									//获取-咨询师职责
									String counselorDuty = userLevel.getCounselorDuty();
									if (counselorDuty != null && !"".equals(counselorDuty)) {
										//判断咨询师职责-是否为空字符串（是否存在咨询师职责）
										if (!counselorDuty.contains(" ")) {
											StringBuilder stringBuilder = new StringBuilder();
											String[] split = counselorDuty.split(",");
											if(split!=null){
												for (String id : split) {
													if (id != null && !"".equals(id)) {
														//获取职责名称
														Duty duty = counselorDutyService.getDutyById(id);
														if (duty != null) {
															stringBuilder.append(duty.getDutyName() + " ");
														}
													}
												}
												//更新-咨询师职责
												userLevel.setCounselorDuty(stringBuilder.toString());
											}
										}
									}
									//更新-文章中用户级别信息
									article.setUserLevel(userLevel);
								}
								//是否已关注作者
								//设置关注量查询条件
								AttentionExample attentionExample = new AttentionExample();
								Criteria createCriteria = attentionExample.createCriteria();
								//被关注者
								createCriteria.andBeAttentionNumEqualTo(article.getCreatBy());//被点击个人文章的用户手机号（主键）
								//关注者
								createCriteria.andAttentionNumEqualTo(phoneNumber);//当前登录人的手机号
								//被点击个人文章的用户是否当前用户关注
								int countAttention = attentionService.countAttention(attentionExample);
								if(countAttention>0){
									//设置关注标识
									article.setIsAttention(1);
								}
							}
						}else{
							allArticleResponse.setDes("");
						}
						allArticleResponse.setArticles(articles);
						allArticleResponse.setSinceId(sinceId);
						allArticleResponse.setMaxId(sinceId + articles.size() - 1);
						allArticleResponse.setRows(articles.size());
						allArticleResponse.setCode("1");
					} else {
						// 重新登录
						allArticleResponse.setCode("2");
						allArticleResponse.setDes("重新登录");
					}
				} else {
					// 重新登录
					allArticleResponse.setCode("2");
					allArticleResponse.setDes("重新登录");
				}
			
			logger.info("返回：" + mapper.writeValueAsString(allArticleResponse));
		} catch (Exception e) {
			e.printStackTrace();
			allArticleResponse.setCode("0");
			allArticleResponse.setDes("系统繁忙");
		}
		return allArticleResponse;
	}
}
